{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b4a86c30-6eb0-451e-ba52-4cd01df2df52",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: http://mirrors.cloud.aliyuncs.com/pypi/simple/\n",
      "Requirement already satisfied: pymilvus==2.5.10 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus[model]==2.5.10) (2.5.10)\n",
      "Requirement already satisfied: openai==1.82.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (1.82.0)\n",
      "Requirement already satisfied: requests==2.32.3 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (2.32.3)\n",
      "Requirement already satisfied: tqdm==4.67.1 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (4.67.1)\n",
      "Requirement already satisfied: torch==2.7.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (2.7.0)\n",
      "Requirement already satisfied: setuptools>69 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus==2.5.10->pymilvus[model]==2.5.10) (78.1.1)\n",
      "Requirement already satisfied: grpcio<=1.67.1,>=1.49.1 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus==2.5.10->pymilvus[model]==2.5.10) (1.67.1)\n",
      "Requirement already satisfied: protobuf>=3.20.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus==2.5.10->pymilvus[model]==2.5.10) (6.31.1)\n",
      "Requirement already satisfied: python-dotenv<2.0.0,>=1.0.1 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus==2.5.10->pymilvus[model]==2.5.10) (1.1.1)\n",
      "Requirement already satisfied: ujson>=2.0.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus==2.5.10->pymilvus[model]==2.5.10) (5.10.0)\n",
      "Requirement already satisfied: pandas>=1.2.4 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus==2.5.10->pymilvus[model]==2.5.10) (2.3.0)\n",
      "Requirement already satisfied: milvus-lite>=2.4.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus==2.5.10->pymilvus[model]==2.5.10) (2.5.1)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai==1.82.0) (4.9.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai==1.82.0) (1.9.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai==1.82.0) (0.28.1)\n",
      "Requirement already satisfied: jiter<1,>=0.4.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai==1.82.0) (0.10.0)\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai==1.82.0) (2.11.7)\n",
      "Requirement already satisfied: sniffio in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai==1.82.0) (1.3.1)\n",
      "Requirement already satisfied: typing-extensions<5,>=4.11 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from openai==1.82.0) (4.14.1)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from requests==2.32.3) (3.4.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from requests==2.32.3) (3.10)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from requests==2.32.3) (2.5.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from requests==2.32.3) (2025.6.15)\n",
      "Requirement already satisfied: filelock in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (3.18.0)\n",
      "Requirement already satisfied: sympy>=1.13.3 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (1.14.0)\n",
      "Requirement already satisfied: networkx in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (3.5)\n",
      "Requirement already satisfied: jinja2 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (3.1.6)\n",
      "Requirement already satisfied: fsspec in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (2025.5.1)\n",
      "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.6.77 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (12.6.77)\n",
      "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.6.77 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (12.6.77)\n",
      "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.6.80 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (12.6.80)\n",
      "Requirement already satisfied: nvidia-cudnn-cu12==9.5.1.17 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (9.5.1.17)\n",
      "Requirement already satisfied: nvidia-cublas-cu12==12.6.4.1 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (12.6.4.1)\n",
      "Requirement already satisfied: nvidia-cufft-cu12==11.3.0.4 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (11.3.0.4)\n",
      "Requirement already satisfied: nvidia-curand-cu12==10.3.7.77 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (10.3.7.77)\n",
      "Requirement already satisfied: nvidia-cusolver-cu12==11.7.1.2 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (11.7.1.2)\n",
      "Requirement already satisfied: nvidia-cusparse-cu12==12.5.4.2 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (12.5.4.2)\n",
      "Requirement already satisfied: nvidia-cusparselt-cu12==0.6.3 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (0.6.3)\n",
      "Requirement already satisfied: nvidia-nccl-cu12==2.26.2 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (2.26.2)\n",
      "Requirement already satisfied: nvidia-nvtx-cu12==12.6.77 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (12.6.77)\n",
      "Requirement already satisfied: nvidia-nvjitlink-cu12==12.6.85 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (12.6.85)\n",
      "Requirement already satisfied: nvidia-cufile-cu12==1.11.1.6 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (1.11.1.6)\n",
      "Requirement already satisfied: triton==3.3.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from torch==2.7.0) (3.3.0)\n",
      "Requirement already satisfied: pymilvus.model>=0.3.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus[model]==2.5.10) (0.3.2)\n",
      "Requirement already satisfied: httpcore==1.* in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from httpx<1,>=0.23.0->openai==1.82.0) (1.0.9)\n",
      "Requirement already satisfied: h11>=0.16 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai==1.82.0) (0.16.0)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pydantic<3,>=1.9.0->openai==1.82.0) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.33.2 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pydantic<3,>=1.9.0->openai==1.82.0) (2.33.2)\n",
      "Requirement already satisfied: typing-inspection>=0.4.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pydantic<3,>=1.9.0->openai==1.82.0) (0.4.1)\n",
      "Requirement already satisfied: numpy>=1.26.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pandas>=1.2.4->pymilvus==2.5.10->pymilvus[model]==2.5.10) (2.3.1)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pandas>=1.2.4->pymilvus==2.5.10->pymilvus[model]==2.5.10) (2.9.0.post0)\n",
      "Requirement already satisfied: pytz>=2020.1 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pandas>=1.2.4->pymilvus==2.5.10->pymilvus[model]==2.5.10) (2025.2)\n",
      "Requirement already satisfied: tzdata>=2022.7 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pandas>=1.2.4->pymilvus==2.5.10->pymilvus[model]==2.5.10) (2025.2)\n",
      "Requirement already satisfied: transformers>=4.36.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (4.53.1)\n",
      "Requirement already satisfied: onnxruntime in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (1.22.0)\n",
      "Requirement already satisfied: scipy>=1.10.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (1.16.0)\n",
      "Requirement already satisfied: six>=1.5 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from python-dateutil>=2.8.2->pandas>=1.2.4->pymilvus==2.5.10->pymilvus[model]==2.5.10) (1.17.0)\n",
      "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from sympy>=1.13.3->torch==2.7.0) (1.3.0)\n",
      "Requirement already satisfied: huggingface-hub<1.0,>=0.30.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (0.33.2)\n",
      "Requirement already satisfied: packaging>=20.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (25.0)\n",
      "Requirement already satisfied: pyyaml>=5.1 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (6.0.2)\n",
      "Requirement already satisfied: regex!=2019.12.17 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (2024.11.6)\n",
      "Requirement already satisfied: tokenizers<0.22,>=0.21 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (0.21.2)\n",
      "Requirement already satisfied: safetensors>=0.4.3 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (0.5.3)\n",
      "Requirement already satisfied: hf-xet<2.0.0,>=1.1.2 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from huggingface-hub<1.0,>=0.30.0->transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (1.1.5)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from jinja2->torch==2.7.0) (3.0.2)\n",
      "Requirement already satisfied: coloredlogs in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from onnxruntime->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (15.0.1)\n",
      "Requirement already satisfied: flatbuffers in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from onnxruntime->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (25.2.10)\n",
      "Requirement already satisfied: humanfriendly>=9.1 in /root/miniconda3/envs/deepseek/lib/python3.13/site-packages (from coloredlogs->onnxruntime->pymilvus.model>=0.3.0->pymilvus[model]==2.5.10) (10.0)\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.\u001b[0m\u001b[33m\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "!pip install \"pymilvus[model]==2.5.10\" openai==1.82.0 requests==2.32.3 tqdm==4.67.1 torch==2.7.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2927d1b9-64cd-486d-9065-671ef2db0779",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "# 从环境变量获取 DeepSeek API Key\n",
    "api_key = os.getenv(\"DEEPSEEK_API_KEY\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5655f063-09d1-4364-97ec-758227de4302",
   "metadata": {},
   "outputs": [],
   "source": [
    "from glob import glob\n",
    "\n",
    "text_lines = []\n",
    "\n",
    "for file_path in glob(\"mfd.md\", recursive=True):\n",
    "    with open(file_path, \"r\") as file:\n",
    "        file_text = file.read()\n",
    "\n",
    "    text_lines += file_text.split(\"* \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "591c9382-c377-4c2f-8785-475f2c981c5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "388"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(text_lines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "373aa29b-5007-49db-835e-414b9d32b162",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI\n",
    "\n",
    "deepseek_client = OpenAI(\n",
    "    api_key=api_key,\n",
    "    base_url=\"https://api.deepseek.com/v1\",  # DeepSeek API 的基地址\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "626b33b6-46ba-436e-ac61-b5b3ff8b815c",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/envs/deepseek/lib/python3.13/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "from pymilvus import model as milvus_model\n",
    "\n",
    "embedding_model = milvus_model.DefaultEmbeddingFunction()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "21891ee6-b36f-454c-90c9-f859835d38ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "768\n",
      "[-0.04836059  0.07163021 -0.01130063 -0.03789341 -0.03320651 -0.01318453\n",
      " -0.03041721 -0.02269495 -0.02317858 -0.00426026]\n"
     ]
    }
   ],
   "source": [
    "test_embedding = embedding_model.encode_queries([\"This is a test\"])[0]\n",
    "embedding_dim = len(test_embedding)\n",
    "print(embedding_dim)\n",
    "print(test_embedding[:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9ef4954a-c6f5-4fd7-995e-33d37693704b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.02752976  0.0608853   0.00388525 -0.00215193 -0.02774976 -0.0118618\n",
      " -0.04020916 -0.06023417 -0.03813156  0.0100272 ]\n"
     ]
    }
   ],
   "source": [
    "test_embedding_0 = embedding_model.encode_queries([\"That is a test\"])[0]\n",
    "print(test_embedding_0[:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e60c6046-2f97-4297-ae3f-01e649188906",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pymilvus import MilvusClient\n",
    "\n",
    "milvus_client = MilvusClient(uri=\"./milvus_mfd.db\")\n",
    "\n",
    "collection_name = \"my_rag_collection\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "634ed395-6642-48c2-b15e-6afb2c776285",
   "metadata": {},
   "outputs": [],
   "source": [
    "if milvus_client.has_collection(collection_name):\n",
    "    milvus_client.drop_collection(collection_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d74417b8-0087-4243-aabe-5f7db03dbab5",
   "metadata": {},
   "outputs": [],
   "source": [
    "milvus_client.create_collection(\n",
    "    collection_name=collection_name,\n",
    "    dimension=embedding_dim,\n",
    "    metric_type=\"IP\",  # 内积距离\n",
    "    consistency_level=\"Strong\",  # 支持的值为 (`\"Strong\"`, `\"Session\"`, `\"Bounded\"`, `\"Eventually\"`)。更多详情请参见 https://milvus.io/docs/consistency.md#Consistency-Level。\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "dd85661f-8a7d-41bd-9cc3-68ed849be51d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Creating embeddings: 100%|██████████| 388/388 [00:00<00:00, 553722.34it/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'insert_count': 388, 'ids': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387], 'cost': 0}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "data = []\n",
    "\n",
    "doc_embeddings = embedding_model.encode_documents(text_lines)\n",
    "print(1)\n",
    "for i, line in enumerate(tqdm(text_lines, desc=\"Creating embeddings\")):\n",
    "    data.append({\"id\": i, \"vector\": doc_embeddings[i], \"text\": line})\n",
    "\n",
    "milvus_client.insert(collection_name=collection_name, data=data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "361c74f1-4e6a-4d47-82a6-997b1fa5afde",
   "metadata": {},
   "outputs": [],
   "source": [
    "question = \"当事人一方不履行合同义务,如何处理\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "342abce8-a875-4e25-afdd-c86d2cff6fe0",
   "metadata": {},
   "outputs": [],
   "source": [
    "search_res = milvus_client.search(\n",
    "    collection_name=collection_name,\n",
    "    data=embedding_model.encode_queries(\n",
    "        [question]\n",
    "    ),  # 将问题转换为嵌入向量\n",
    "    limit=3,  # 返回前3个结果\n",
    "    search_params={\"metric_type\": \"IP\", \"params\": {}},  # 内积距离\n",
    "    output_fields=[\"text\"],  # 返回 text 字段\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d77d597a-b2bf-484e-bd7d-ca3853d6eb2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "    [\n",
      "        \"\\u6cd5\\u5f8b\\u53e6\\u6709\\u89c4\\u5b9a\\u7684\\uff0c\\u4f9d\\u7167\\u5176\\u89c4\\u5b9a\\u3002\",\n",
      "        1.0\n",
      "    ]\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "retrieved_lines_with_distances = [\n",
    "    (res[\"entity\"][\"text\"], res[\"distance\"]) for res in search_res[0]\n",
    "]\n",
    "print(json.dumps(retrieved_lines_with_distances, indent=4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "8d0aeaa0-0d46-40d8-956d-b0cb4ce1678f",
   "metadata": {},
   "outputs": [],
   "source": [
    "context = \"\\n\".join(\n",
    "    [line_with_distance[0] for line_with_distance in retrieved_lines_with_distances]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e9505466-c6eb-4fb9-8279-e88012ff3953",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'法律另有规定的，依照其规定。'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "context"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ebf284b2-fe30-4436-8f24-8a1b5ed80f14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'当事人一方不履行合同义务,如何处理'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "question"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d2f032bf-d03d-4cda-8193-e8fa753aff1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "SYSTEM_PROMPT = \"\"\"\n",
    "Human: 你是一个 AI 助手。你能够从提供的上下文段落片段中找到问题的答案。\n",
    "\"\"\"\n",
    "USER_PROMPT = f\"\"\"\n",
    "请使用以下用 <context> 标签括起来的信息片段来回答用 <question> 标签括起来的问题。\n",
    "<context>\n",
    "{context}\n",
    "</context>\n",
    "<question>\n",
    "{question}\n",
    "</question>\n",
    "<translated>\n",
    "</translated>\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "94a07776-c17d-47f0-8a5a-40cee2b6539b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n请使用以下用 <context> 标签括起来的信息片段来回答用 <question> 标签括起来的问题。\\n<context>\\n法律另有规定的，依照其规定。\\n</context>\\n<question>\\n当事人一方不履行合同义务,如何处理\\n</question>\\n<translated>\\n</translated>\\n'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "USER_PROMPT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0ad16c28-463b-48e5-8b2f-6382812d413d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "根据提供的上下文片段\"法律另有规定的，依照其规定\"，这表示当出现合同义务不履行的情况时，应优先适用相关法律中的具体规定。不过，该片段并未直接回答合同违约的具体处理方式。\n",
      "\n",
      "完整的回答通常应包含：\n",
      "1. 继续履行\n",
      "2. 采取补救措施\n",
      "3. 赔偿损失等常见处理方式\n",
      "\n",
      "由于提供的上下文信息有限，建议参考《民法典》合同编中关于违约责任的具体条款（如第577条等）来获取完整解答。\n"
     ]
    }
   ],
   "source": [
    "response = deepseek_client.chat.completions.create(\n",
    "    model=\"deepseek-chat\",\n",
    "    messages=[\n",
    "        {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n",
    "        {\"role\": \"user\", \"content\": USER_PROMPT},\n",
    "    ],\n",
    ")\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "65e9841e-419a-4493-9ab8-614d1738b5c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'当事人一方不履行合同义务,如何处理'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "question"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c6972e89-4cf6-4984-a06c-ee5f5998fac0",
   "metadata": {},
   "outputs": [],
   "source": [
    "question = \"不动产登记由谁办理\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d8b7224e-8681-462c-ad82-6a21affbf454",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'不动产登记由谁办理'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "question"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "0f53799a-e5ed-40f2-a692-6cf1ff2665d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'法律另有规定的，依照其规定。'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "context"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9ed9daa0-024d-4025-9e7a-a90b4e23ce8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "search_res = milvus_client.search(\n",
    "    collection_name=collection_name,\n",
    "    data=embedding_model.encode_queries(\n",
    "        [question]\n",
    "    ),  # 将问题转换为嵌入向量\n",
    "    limit=3,  # 返回前3个结果\n",
    "    search_params={\"metric_type\": \"IP\", \"params\": {}},  # 内积距离\n",
    "    output_fields=[\"text\"],  # 返回 text 字段\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "b7eb303c-cbef-456a-bcea-8896b68de4ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "    [\n",
      "        \"\\u571f\\u5730\\u627f\\u5305\\u7ecf\\u8425\\u6743\\u4eba\\u4f9d\\u6cd5\\u5bf9\\u627f\\u5305\\u5730\\u4eab\\u6709\\u5360\\u6709\\u3001\\u4f7f\\u7528\\u3001\\u6536\\u76ca\\u7684\\u6743\\u5229\\u3002\\n\\u571f\\u5730\\u627f\\u5305\\u7ecf\\u8425\\u6743\\u4eba\\u53ef\\u4ee5\\u91c7\\u53d6\\u51fa\\u79df\\u3001\\u5165\\u80a1\\u6216\\u8005\\u5176\\u4ed6\\u65b9\\u5f0f\\u6d41\\u8f6c\\u571f\\u5730\\u7ecf\\u8425\\u6743\\u3002\\n\\n**\\u7b2c\\u4e09\\u767e\\u516d\\u5341\\u4e00\\u6761*\",\n",
      "        0.694697380065918\n",
      "    ],\n",
      "    [\n",
      "        \"\\u5171\\u540c\\u5171\\u6709\\u4eba\\u5bf9\\u5171\\u6709\\u7684\\u4e0d\\u52a8\\u4ea7\\u6216\\u8005\\u52a8\\u4ea7\\u5171\\u540c\\u4eab\\u6709\\u6240\\u6709\\u6743\\u3002\\n\\u5171\\u540c\\u5171\\u6709\\u4eba\\u5bf9\\u5171\\u6709\\u8d22\\u4ea7\\u4eab\\u6709\\u5e73\\u7b49\\u7684\\u5360\\u6709\\u3001\\u4f7f\\u7528\\u3001\\u6536\\u76ca\\u548c\\u5904\\u5206\\u7684\\u6743\\u5229\\u3002\\n\\n**\\u7b2c\\u4e09\\u767e\\u4e00\\u5341\\u4e09\\u6761*\",\n",
      "        0.694697380065918\n",
      "    ],\n",
      "    [\n",
      "        \"\\u4e1a\\u4e3b\\u5e94\\u5f53\\u9075\\u5b88\\u6cd5\\u5f8b\\u3001\\u6cd5\\u89c4\\u4ee5\\u53ca\\u7ba1\\u7406\\u89c4\\u7ea6\\u3002\\n\\u7ba1\\u7406\\u89c4\\u7ea6\\u5bf9\\u5168\\u4f53\\u4e1a\\u4e3b\\u5177\\u6709\\u7ea6\\u675f\\u529b\\u3002\\n\\n**\\u7b2c\\u4e8c\\u767e\\u516b\\u5341\\u4e94\\u6761*\",\n",
      "        0.694697380065918\n",
      "    ]\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "retrieved_lines_with_distances = [\n",
    "    (res[\"entity\"][\"text\"], res[\"distance\"]) for res in search_res[0]\n",
    "]\n",
    "print(json.dumps(retrieved_lines_with_distances, indent=4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "1e2e46c2-0653-4f4d-9d0c-b15b35f5cc1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "context = \"\\n\".join(\n",
    "    [line_with_distance[0] for line_with_distance in retrieved_lines_with_distances]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "f74f6b26-ebec-4f0e-a295-9d2eb4017584",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'土地承包经营权人依法对承包地享有占有、使用、收益的权利。\\n土地承包经营权人可以采取出租、入股或者其他方式流转土地经营权。\\n\\n**第三百六十一条*\\n共同共有人对共有的不动产或者动产共同享有所有权。\\n共同共有人对共有财产享有平等的占有、使用、收益和处分的权利。\\n\\n**第三百一十三条*\\n业主应当遵守法律、法规以及管理规约。\\n管理规约对全体业主具有约束力。\\n\\n**第二百八十五条*'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "context"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "623caa59-759c-403d-90d8-99286d5a8fd7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
